package com.fmi.timedealer.reporting;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fmi.timedealer.R;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;
import com.fmi.timedealer.reporting.model.ActivitiesPerCategoryReportItem;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class ActivitiesPerCategoryReportActivity extends Activity {

	public static final String SELECTED_CATEGORY = "SelectedCatgory";
	private IDataManager dbManager = null;
	
	@SuppressWarnings("static-access")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.pie_chart);
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		String selectedCategory = (String) extras.getSerializable(SELECTED_CATEGORY);
		selectedCategory = extras.getString(SELECTED_CATEGORY);
		List<ActivitiesPerCategoryReportItem> l = dbManager.getActivitiesPieChart(selectedCategory);
		
		int wholeTrackedTime = 0;
		List<String> items = new ArrayList<String>();
		
		for(ActivitiesPerCategoryReportItem ri : l) {
			wholeTrackedTime += ri.getSpentTime();
			items.add(ri.getActivityName());
		}
		
		Map<String, Double> activityToPercent = new HashMap<String, Double>();
		
		for(ActivitiesPerCategoryReportItem r : l) {
			Double ddd = r.getSpentTime()/wholeTrackedTime*100;
	        DecimalFormat twoDForm = new DecimalFormat("#.##");
	        ddd = Double.valueOf(twoDForm.format(ddd));
			activityToPercent.put(r.getActivityName(), new Double(ddd));
		}
		
		String data = "";
		String labels = "";
						
		for(String key : activityToPercent.keySet()) {
			data += (activityToPercent.get(key)+",");
			labels += (key+"|");
		}
		
		if(data.length() != 0 && labels.length() != 0) {
			data = data.copyValueOf(data.toCharArray(), 0, data.length()-1);
			labels = labels.copyValueOf(labels.toCharArray(), 0, labels.length()-1);
		}

		String googleChartURL = "http://chart.apis.google.com/chart";
		String chartSize = "?chs=300x225";
		String chartType = "&cht=p3";
		String chartColors = "&chco=FFCC33,EE2C2C,9900CC,9AFF9A,0000FF,E7E643,33FF33";
		String chartData = "&chd=t:" + data;
		String chartDataLabels = "&chdl="+labels;
		String chartDataLabelsPosition = "&chdlp=b";
		String chartTitle = "&chtt=Report by activities for category +" + selectedCategory;
		
		WebView googleChartView = new WebView(this);
		setContentView(googleChartView);
		//String mUrl = "http://chart.apis.google.com/chart?chs=300x225&cht=p3&chd=s:SfGG&chdl=Play+Tennis|Play+Football|Swimming|Play+Basket&chdlp=l&chp=0.6&chtt=Category+Sport";
		String mUrl = googleChartURL+chartSize+chartType+chartColors+chartData+chartDataLabels+chartDataLabelsPosition+chartTitle;
		googleChartView.loadUrl(mUrl);	
		
	}
}
